package com.xqcx.libra.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 传入mq 中tabkeName ,判断表名后面是否是数字, 是数字 -分表
 * 去掉数字后缀
 * 分表表名--> 去掉后面数字
 * </p>
 * @author zhengfei
 * @create 2018-01-10 10:22
 **/
public class ShardTableNameProcess {
    private final static String REGEX="[0-9]*";
    private final static String UNDERLINE="_";

    public static String converter(String mqTableName) {
        String[] strs = mqTableName.split(UNDERLINE);
        StringBuffer sb = new StringBuffer();
        for(String str : strs){
            if(!isNumeric(str)){
                sb.append(str);
                sb.append(UNDERLINE);
            }
        }
        String tableName = sb.toString();
        if(tableName.endsWith(UNDERLINE)){
            tableName = tableName.substring(0 , tableName.length()-1);
        }
        return tableName;
    }

    private static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile(REGEX);
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
}
